package com.example.wenliangjiudian.controller;

import com.example.wenliangjiudian.entity.Menu;
import com.example.wenliangjiudian.entity.User;
import com.example.wenliangjiudian.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MenuController {

    @Autowired
    private MenuService menuService;


    @RequestMapping("/menu/selectAll")
    @ResponseBody
    private Object selectAll() {
        List<Menu> list = menuService.selectAll();
        for (Menu menu : list) {
            menu.setList(new ArrayList<>());
        }
        return list;
    }

    @RequestMapping("/menu/getMyMenu")
    @ResponseBody
    private Object getMyMenu(HttpServletRequest request) {
        //登录成功
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null) {
            return new ArrayList<>();
        }
        Integer id = loginUser.getId();
        List<Menu> list = menuService.getMyMenu(id);
        for (Menu menu : list) {
            if (menu == null) {
                continue;
            }
            menu.setList(new ArrayList<>());
        }
        return list;

    }
}
